import React from "react";

import { ajax ,showResultMessage} from '../../common';

import {UIDataTableView} from '../../uicomponents/UIDataComponentView';

import UIFormView from '../../uicomponents/UIFormView';

/** 
 * 产品模型数据类型映射管理
*/
export default class ProductDatabaseDataTypeMapApp extends React.Component {

	render() {
		return (
			<ProductDatabaseDataTypeMapListPanel />
		);
	}
}

/**
 * 产品模型数据类型映射列表
 */
class ProductDatabaseDataTypeMapListPanel extends React.Component {

	constructor(props) {
		super(props);
		this.state = {
			formTitle: '',
		}
	}

	actionHandler = (actionEvent) => {
		const { dataTypeMapListRef, selectDataTypeMapIds, dataTypeMapFormRef } = this;
		return {
			refresh: () => {
				dataTypeMapListRef.loadData();
			},
			addNewDataTypeMap: () => {
				this.setState({ formTitle: '新增数据库数据类型映射' });
				setTimeout(() => {
					dataTypeMapFormRef.openModal();
				});
			},
			editDataTypeMap: () => {
				this.setState({ formTitle: '编辑数据库数据类型映射' });
				setTimeout(() => {
					dataTypeMapFormRef.openModal(() => {
						dataTypeMapFormRef.setFormData(actionEvent.params);
					});
				});
			},
			doSave: () => {
				dataTypeMapFormRef.getFormData((formData) => {
					ajax('PRODUCT/MODEL/productDatabaseDataTypeMap/saveDataTypeMap', formData, (data) => {
						dataTypeMapFormRef.closeModal();
						dataTypeMapListRef.loadData();
						showResultMessage({
							success: true,
							message: 'message.saveSuccess',
						});
					});
				});
			},
			deleteDataTypeMaps : () => {
				let selectedIds = null;

				if (actionEvent.params != null) {
					selectedIds = [actionEvent.params.id];
				}
				else {
					selectedIds = selectDataTypeMapIds;
				}

				ajax('PRODUCT/MODEL/productDatabaseDataTypeMap/deleteDataTypeMap', { ids: selectedIds }, (data) => {
					dataTypeMapListRef.loadData();
				});
			}
		};
	}

	render() {

		const dataTypeMapTableProps = {
			ref: (inst) => { this.dataTypeMapListRef = inst },
			actionHandler: this.actionHandler,
			actionGroupId: 'dataTypeMapListActionGroup',
			rowActionGroupId: 'dataTypeMapRowActionGroup',
			tableConfigId: 'productDatabaseDatatTypeMapTable',
			dataUrl: 'PRODUCT/MODEL/productDatabaseDataTypeMap/queryDataTypeMapList',
			selectMode: 'multi',
			searchFormId: "dataTypeMapQueryForm",
			autoLoad: true,
			bordered: false,
			setToolbarState: (topToolBar, selectedIds) => {
				if (selectedIds.length > 0) {
					topToolBar.enable('delete');
					topToolBar.enable('edit');
				}
				else {
					topToolBar.disable('delete');
					topToolBar.disable('edit');
				}
			},
			onSelectChange:
				(selectRowKeys) => {
					this.selectDataTypeMapIds = selectRowKeys;
				}
			,
		};

		const dataTypeMapFormProps = {
			formConfigId: 'dataTypeMapForm',
			actionGroupId: 'saveActionGroup',
			actionHandler: this.actionHandler,
			ref: (inst) => { this.dataTypeMapFormRef = inst },
			inPop: true,
			destroyOnClose: true,
			title: this.state.formTitle,
		}

		return (
			<UIDataTableView {...dataTypeMapTableProps}>
				<UIFormView {...dataTypeMapFormProps} />
			</UIDataTableView>
		);
	}
}